#!/usr/bin/env bash

# setcom.sh
#
# Purpose: This scripts sets the motecom.txt file
#
#


AC2_WEB_ROOT=..
AC2_WEB_MOTE_IFACE=$AC2_WEB_ROOT/MoteInterface
AC2_MOTECOM=$AC2_WEB_MOTE_IFACE/motecom.txt
AC2_MOTECOM_LOG=setcom.log


#if [ ! -f $AC2_MOTECOM_LOG ]; then
#    touch $AC2_MOTECOM_LOG
#fi


log() {
    echo $(date +%m%d%y.%H%M%S): $@ 
    # >> $AC2_MOTECOM_LOG
}

# check for motecom file
if [ ! -f $AC2_MOTECOM ]; then        
    log "ERROR Cannot find file $AC2_MOTECOM"
    exit 1
fi

# use motelist command to find mote interface
MOTE_IFACE=`motelist | tail -1 | awk '{print $2}'`
MOTE_IFACE="serial@$MOTE_IFACE:telosb"
if [ -z $MOTE_IFACE ]; then 
    log "ERROR motelist command did any interface"
    exit 1
fi

# write motecom
# WebApp need to know interface for PacketSender.jar
echo $MOTE_IFACE > $AC2_MOTECOM